package com.fmatos.amazingwallpapers.wallpaper.domain;

import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class WallpaperDrawing implements AmazingWallpaper {

	private static final String TAG = WallpaperDrawing.class.getSimpleName();

	private int x,y;
	private Paint paint = new Paint();

	private MyPoint point;

	private Path path;

	public WallpaperDrawing(SharedPreferences prefs) {

		paint.setAntiAlias(true);
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.STROKE);
		paint.setStrokeJoin(Paint.Join.ROUND);
		paint.setStrokeWidth(10f);

	}

	@Override
	public void onSurfaceChanged(SurfaceHolder holder,int width, int height) {
		Log.i(TAG,"Surface changed");

	}

	@Override
	public void onTouchEvent(MotionEvent event, SurfaceHolder holder) {

		x = (int) event.getX();
		y = (int) event.getY();

		point  = new MyPoint("aaaa", x, y);
		drawLine(event,holder);

	}

	private void drawLine(MotionEvent event, SurfaceHolder holder) {
		Canvas canvas = null;

		if ( event.getAction() == MotionEvent.ACTION_UP ) {
			path = null;
			return;
		}

		try{	
			if ( point != null ) {
				canvas = holder.lockCanvas();
				canvas.drawColor(Color.BLACK);
				if ( path == null ) {
					path = new Path();
					path.moveTo(x, y);
				} else {
					path.lineTo(x, y);
				}

				canvas.drawPath(path , paint);
			}
		} finally {
			if (canvas != null)
				holder.unlockCanvasAndPost(canvas);
		}

	}

	public void drawCircle(SurfaceHolder holder) {

		Canvas canvas = null;
		try{
			if ( point != null ) {
				canvas = holder.lockCanvas();
				canvas.drawCircle(point.getX(),point.getY() ,20.0f, paint);
			}
		} finally {
			if (canvas != null)
				holder.unlockCanvasAndPost(canvas);
		}
	}

	@Override
	public void draw(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onVisibilityChanged(boolean visible) {
		// TODO Auto-generated method stub
		
	}

}
